﻿Imports System.Windows.Threading

Class WpfImageViewer

    Public Event DoubleClickOnImage()
    Public Event ClickOnImage()

    Public Sub SetImage(ByVal image As BitmapSource)
        PrivateSetImage(image)
        'Dim dlg As New DelegatePrivateSetImage(AddressOf PrivateSetImage)
        'Image1.Dispatcher.BeginInvoke(DispatcherPriority.Render, dlg, image)
    End Sub

    Private Delegate Sub DelegatePrivateSetImage(ByVal image As BitmapSource)
    Private Sub PrivateSetImage(ByVal image As BitmapSource)
        Try
            Image1.Source = image
        Catch ex As Exception
            MessageBox.Show("The following exception occured updating the image:" & vbCrLf & vbCrLf & ex.Message, ex.GetType().ToString, MessageBoxButton.OK, MessageBoxImage.Error)
        End Try
    End Sub

    Private Sub ImageViewer2_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'PrivateSetImage(Me.Image1.Source)
    End Sub

    Private Sub ImageViewer2_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDoubleClick
        RaiseEvent DoubleClickOnImage()
    End Sub

    Private Sub ImageViewer2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseUp
        RaiseEvent ClickOnImage()
    End Sub
End Class
